<%@page import="com.dotmedia.entities.SystemEntity"%>
<%@page import="com.dotmedia.dboperations.SystemDatabaseOperations"%>
<%@page import="com.dotmedia.dboperations.AdTemplateDatabaseOperations"%>
<%@page import="com.dotmedia.entities.AdTemplate"%>
<%@page import="com.dotmedia.dboperations.AdsizeDatabaseOperations"%>
<%@page import="com.dotmedia.entities.Adsize"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add New Ad</title>
<link rel="stylesheet" type="text/css" href="css/astele.css">
<link type="text/css" href="css/jquery-ui-1.8.10.custom.css" rel="stylesheet" />
<link href="javascript/uploadify/uploadify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="javascript/uploadify/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="javascript/uploadify/swfobject.js"></script>
<script type="text/javascript" src="javascript/uploadify/jquery.uploadify.v2.1.4.min.js"></script>
<script type="text/javascript" src="javascript/jquery-ui-1.8.10.custom.min.js"></script>
<script type="text/javascript" src="javascript/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
	function uploadifyFields() {
		jQuery('input[name^=genupload_]').uploadify({
		    'uploader'  : 'javascript/uploadify/uploadify.swf',
		    'script'    : 'javascript/uploadify/uploadify.php',
		    'cancelImg' : 'javascript/uploadify/cancel.png',
		    'folder'    : '/',
		    'auto'      : true
		});		
		jQuery('input[name^=swfupload_]').uploadify({
		    'uploader'  : 'javascript/uploadify/uploadify.swf',
		    'script'    : 'javascript/uploadify/uploadify.php',
		    'cancelImg' : 'javascript/uploadify/cancel.png',
		    'folder'    : '/',
		    'fileExt'   : '*.swf;',
		    'fileDesc'  : 'SWF files',
		    'auto'      : true
		});	
		jQuery('input[name^=imgupload_]').uploadify({
		    'uploader'  : 'javascript/uploadify/uploadify.swf',
		    'script'    : 'javascript/uploadify/uploadify.php',
		    'cancelImg' : 'javascript/uploadify/cancel.png',
		    'folder'    : '/',
		    'fileExt'   : '*.jpeg;*.gif;*.png',
		    'fileDesc'  : 'Image files',
		    'auto'      : true
		});	
		jQuery('input[name^=dimgupload_]').uploadify({
		    'uploader'  : 'javascript/uploadify/uploadify.swf',
		    'script'    : 'javascript/uploadify/uploadify.php',
		    'cancelImg' : 'javascript/uploadify/cancel.png',
		    'folder'    : '/',
		    'fileExt'   : '*.jpeg;*.gif;*.png',
		    'fileDesc'  : 'Image files',
		    'auto'      : true
		});	
	}
</script>
<script type="text/javascript">
	function validateFields() {
		//this function will validate the fields and return true / false - this function will be called when the form is submitted
		//advertiserId
		jQuery("#advertiserId").css("background-color", "white");
		jQuery("#validationMessage").html("");
		var advertiserId = jQuery("#advertiserId").val();
		if (validateFieldOnLength(advertiserId, 1) == false) {
			jQuery("#validationMessage").html("Advertiser Id is null");
			jQuery("#advertiserId").css("background-color", "red");
			return false;
		}
		//check whether the advertiser id entered exists or not
		advertiserId = jQuery("#advertiserId").val();
		jQuery.ajax({
			type: "GET",
			url: "advertiserIdValidationServlet.do",
			data: "advertiserId="+advertiserId,
			success: function(result) {
				result = String.trim(result);
				if (result == "exists") {
					return true;
				} else {
					jQuery("#validationMessage").html("AdvertiserId is not correct");
					jQuery("#advertiserId").css("background-color", "red");
					return false;
				}
			}
		});
		//get ad ref code and system id
		var adRefCode = jQuery("#adRefCode").val();
		adRefCode = String.trim(adRefCode);
		var systemId = jQuery("#systemId").val();
		systemId = String.trim(systemId);
		jQuery("#adRefCode").css("background-color", "white");
		//now get whether the pair is unique or not
		jQuery.ajax({
			type: "POST",
			url: "adRefCodeAndSystemIdValidationServlet.do",
			data: "adRefCode="+adRefCode+"&systemId="+systemId,
			success: function(result) {
				result = String.trim(result);
				if (result == "unique") {
					//submit the form
					jQuery("#addNewAdForm").submit();
				} else {
					jQuery("#validationMessage").html("The pair (adRefCode, SystemId) is already present in database");
					jQuery("#adRefCode").css("background-color", "red");
					return false;
				}
			} 
		});
		return false;
	}
	function validateFieldOnLength(field, len) {
		if (field.length < len) {
			return false;
		} else {
			return true;
		}
	}
</script>
<%
	String advertisersAutocomplete = (String) request.getAttribute("advertiserAutocompleteString");
%>
<script type="text/javascript">
	jQuery(function() {
		var availableAdvertisers = [<%=advertisersAutocomplete%>];
		jQuery("#advertiserId").autocomplete({
			source: availableAdvertisers
		});
	});
	jQuery(function() {
		populateTemplateCode();
	});
	function populateTemplateCode() {
		var adTemplateId = jQuery("#template").val();
		//now we have to make the ajax request to templateCodeServlet.do
		jQuery.ajax({
			type: "GET",
			url: "templateCodeServlet.do",
			data: "adTemplateId="+adTemplateId,
			success: function(result) {
				jQuery("#templateCode").html(result);
				jQuery.ajax({
					type: "POST",
					url: "macroFieldsServlet.do",
					data: "templateCode="+result,
					success: function(result) {
						jQuery("#macroFormDiv").html(result);
						tinyMCE.init({
							mode : "textareas",
						    theme : "advanced",
						    plugins : "emotions,spellchecker,advhr,insertdatetime,preview", 
						            
						    // Theme options - button# indicated the row# only
						    theme_advanced_buttons1 : "newdocument,|,bold,italic,underline,|,justifyleft,justifycenter,justifyright,fontselect,fontsizeselect,formatselect",
						    theme_advanced_buttons2 : "cut,copy,paste,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,anchor,image,|,code,preview,|,forecolor,backcolor",
						    theme_advanced_buttons3 : "insertdate,inserttime,|,spellchecker,advhr,,removeformat,|,sub,sup,|,charmap,emotions",      
						    theme_advanced_toolbar_location : "top",
						    theme_advanced_toolbar_align : "left",
						    theme_advanced_statusbar_location : "bottom",
						    editor_selector : "mceEditor",
						    theme_advanced_resizing : false

						});
						uploadifyFields();
					}
				});
			}
		});
	}
</script>
</head>
<body>
<br />
<br />
<center>
<form name="addNewAdFrom" id="addNewAdForm" method = "POST" action = "addNewAd.action" onsubmit="return validateFields()">
<div id="validationMessage"></div>
<table name="addNewAd" id="addNewAd" class="mt_table">
	<tr class="even_td">
		<td><b>Ad id: </b></td>
		<td><b>New ad</b></td>
	</tr>
	<tr class="odd_td">
		<td><b>Advetiser id: </b></td>
		<td><input type="text" name="advertiserId" id="advertiserId" style="width: 100%"></input></td>
	</tr>
	<tr class="even_td">
		<td><b>Description: </b></td>
		<td><input type="text" name="description" id="description" style="width: 100%"></input></td>
	</tr>
	<tr class="odd_td">
		<td><b>Ad ref code: </b></td>
		<td><input type="text" name="adRefCode" id="adRefCode" style="width: 100%"></input></td>
	</tr>
	<tr class="even_td">
		<td><b>Status: </b></td>
		<td><input type="radio" name="status" value="active" checked>Active 
			<input type="radio" name="status" value="inactive">Inactive 
			<input type="radio" name="status" value="archived">Archived
		</td>
	</tr>
	<tr class="odd_td">
		<td><b>System: </b></td>
		<td><select name="systemId" id="systemId">
			<%
				SystemDatabaseOperations systemDatabaseOperations = new SystemDatabaseOperations();
				List<SystemEntity> allSystemEntities = systemDatabaseOperations.getAllSystemEntities();
				for (SystemEntity currentSystemEntity : allSystemEntities) {
			%>
			<option value="<%=currentSystemEntity.getId()%>"><%=currentSystemEntity.getId()%></option>

			<%
				}
			%>
		</select></td>
	</tr>
	<tr class="even_td">
		<td><b>Adsize: </b></td>
		<%
			//get all the adsize objects
			List<Adsize> allAdsizeEntities = new AdsizeDatabaseOperations().getAllAdsizeEntites();
		%>
		<td><select name="adsize" id="adsize">
			<%
				for (int allAdsizeEntitiesIndex = 0; allAdsizeEntitiesIndex < allAdsizeEntities.size(); allAdsizeEntitiesIndex++) {
					Adsize currentAdsizeEntity = allAdsizeEntities.get(allAdsizeEntitiesIndex);
					String currentValue = "" + currentAdsizeEntity.getAdsizeId();
					String currentLabel = currentAdsizeEntity.getDescription() + " [" + currentAdsizeEntity.getAdsizeRefCode() + "]";
			%>
			<option value="<%=currentValue%>"><%=currentLabel%></option>
			<%
				}
			%>
		</select></td>
	</tr>
	<tr class="odd_td">
		<td><b>Template: </b></td>
		<%
			List<AdTemplate> allAdTemplates = new AdTemplateDatabaseOperations().getAllAdTemplateEntities();
		%>
		<td><select name="template" id="template" onchange="populateTemplateCode()">
			<%
				for (int allAdTemplatesIndex = 0; allAdTemplatesIndex < allAdTemplates.size(); allAdTemplatesIndex++) {
					AdTemplate currentAdTemplate = allAdTemplates.get(allAdTemplatesIndex);
					String currentAdTplValue = "" + currentAdTemplate.getAdtplId();
					String currentAdTplLabel = currentAdTemplate.getDescription() + " [" + currentAdTemplate.getAdtplRefCode() + "]";
			%>
			<option value="<%=currentAdTplValue%>"><%=currentAdTplLabel%></option>
			<%
				}
			%>
		</select></td>
	</tr>
	<tr class="even_td">
		<td><b>Code: </b></td>
		<td><textarea name="templateCode" id="templateCode" rows="20" cols="60" disabled></textarea></td>
	</tr>
</table>
<div id="macroFormDiv"></div>
<input type="submit" value="Save"></input> <input type="reset" value="Cancel"></input></form>
</center>
</body>
</html>